<?php


namespace App\controllers\admin;


use App\controllers\Base;
use Helper\Config;
use Helper\Output;
use Helper\Mysql;
use Firebase\JWT\JWT;
use Helper\Reg;

class Authority extends Base
{

    /**
     * 获取权限列表
     * @param $request
     * @param $response
     * @return false|string
     * @throws \Exception
     */
    public function index($request, $response){

        $list = Mysql::getInstance()->get('authority');
        $menus =  make_tree($list,'id','pid','child');
        return Output::json(200,$menus);

    }

    public function create($request, $response){

        if($request->files){
            $file_name = $this->uploadImgs($request ,'icon','imgs');
            if($file_name ){
                $request->post['icon'] = $file_name;
            }
            $file_name = $this->uploadImgs($request ,'icon_active','imgs');
            if($file_name ){
                $request->post['icon_active'] = $file_name;
            }
        }
        $res = Mysql::getInstance()->insert('authority',$request->post);
        if($res){
            return Output::json(200);
        }
        return Output::json(400);
    }

    public function edit($request, $response){

        $id = $request->post['id'];
        unset($request->post['id']);
        if($request->files){
            $file_name = $this->uploadImgs($request ,'icon','imgs');
            if($file_name ){
                $request->post['icon'] = $file_name;
            }
            $file_name = $this->uploadImgs($request ,'icon_active','imgs');
            if($file_name ){
                $request->post['icon_active'] = $file_name;
            }
        }
        $res = Mysql::getInstance()->where('id',$id)->update('authority',$request->post);
        if($res){
            return Output::json(200);
        }
        return Output::json(400);
    }



}